/************************* (C) COPYRIGHT 2008 Domain **************************
 * File Name   : timer.c                                                      *
 * Author      : Domain                                                       *
 * E-mail      : do.main@msn.com                                              *
 * Version     : 1.0                                                          *
 * Date        : 2008/6/26                                                    *
 * Description :                                                              *
 ******************************************************************************/

#include "timer.h"
#include "config.h"

#ifndef BOOTLOADER
#include "lwip/sys.h"
#include "lwip/memp.h"
#endif /* BOOTLOADER */

vu32 SystemTick = 0;

#ifndef BOOTLOADER
static struct sys_timeouts *SysTimeOut = NULL;

/*******************************************************************************
* Function Name  : SystemTimerInit
* Description    : Initialize the system timer.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SystemTimerInit()
{
  SysTimeOut = sys_arch_timeouts();
}

/*******************************************************************************
* Function Name  : CheckTimeOut
* Description    : Check the timeout function and run it.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void CheckTimeOut()
{
  struct sys_timeo *t, *prev_t, *next;
  static u32 LastTick = 0;
  u32 Diff = SystemTick - LastTick;

  if (Diff > 0)
  {	 
    LastTick = SystemTick;
    if( SysTimeOut != NULL )
    {
      //t = SysTimeOut->next;
      for (t = SysTimeOut->next, prev_t = NULL, next = NULL; t != NULL; t = next) 
      {
        next = t->next;
        if( t->time > Diff)
        {
	        t->time -= Diff;
          prev_t = t;
        }
	      else
        {
          if (prev_t != NULL)
            prev_t->next = t->next;
          else
            SysTimeOut->next = t->next;
    	    t->h(t->arg);
          memp_free(MEMP_SYS_TIMEOUT, t);
        }
      }  
    }
  }
}
#endif /* BOOTLOADER */

/*******************************************************************************
* Function Name  : Delay
* Description    : Inserts a delay time.
* Input          : nTime: specifies the delay time length, in milliseconds.
* Output         : None
* Return         : None
*******************************************************************************/
void Delay(u16 ms)
{ 
  vu32 StartTick = SystemTick;

  while(SystemTick - StartTick < ms)
  {
  }
}

/*******************************************************************************
* Function Name  : Sleep
* Description    : Sleep for a time.
* Input          : - ms: specifies the delay time length, in milliseconds.
* Output         : None
* Return         : None
*******************************************************************************/
void Sleep(u16 ms)
{ 
  vu32 StartTick = SystemTick;

  while(SystemTick - StartTick < ms)
  {
    #ifndef BOOTLOADER
    CheckTimeOut();
    #endif /* BOOTLOADER */
  }
}

/*******************************************************************************
* Function Name  : UpdateTick
* Description    : Update the system tick
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void UpdateTick()
{
  SystemTick += SYSTICK_TIMES;
}

/*******************************************************************************
* Function Name  : GetSysTick
* Description    : Get the SystemTick variable.
* Input          : None
* Output         : None
* Return         : SystemTick
*******************************************************************************/
u32 GetSysTick()
{
  return SystemTick;
}

/*u32 seconds = 0;

void UpdateTime()
{
  seconds = RTC_GetCounter();
  if(seconds == 0x00015180)
  {
    //SystemTime.day++;
	  seconds = 0;
    RTC_SetCounter(0x0);
    RTC_WaitForLastTask();
  }  
}*/

/************************* (C) COPYRIGHT 2008 Domain ******* END OF FILE ******/
